<?php
namespace app\mall_back\controller;


use app\data\model\ShopGoodsCate;
use app\mall_back\model\GoodsModel;
use think\admin\Controller;
use think\admin\extend\CodeExtend;

class Goods extends Controller{


    /**
     * 商品数据管理
     * @auth true
     */
    public function index(){
        $this->title = '商品数据管理';
        $query = GoodsModel::mQuery();
        // 加载对应数据
        $this->type = $this->request->get('type', 'index');
        if ($this->type === 'index') $query->where(['deleted' => 0]);
        elseif ($this->type === 'recycle') $query->where(['deleted' => 1]);
        else $this->error("无法加载 {$this->type} 数据列表！");

        // 列表排序并显示
        $query->order('create_time desc,id desc')->page();
    }

    /**
     * 添加商品数据
     * @auth true
     */
    public function add()
    {
        $this->mode = 'add';
        $this->title = '添加商品数据';
        // 加载对应数据
        GoodsModel::mForm('form');
    }


//    /**
//     * 数据列表处理
//     * @param array $data
//     * @throws \think\db\exception\DataNotFoundException
//     * @throws \think\db\exception\DbException
//     * @throws \think\db\exception\ModelNotFoundException
//     */
//    protected function _page_filter(array &$data)
//    {
//        $this->marks = ShopGoodsMark::items();
//        $this->cates = ShopGoodsCate::treeTable();
//        GoodsService::bindData($data, false);
//    }


    /**
     * 表单数据处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function _form_filter(array &$data)
    {
        if (empty($data['code'])) {
            $data['code'] = CodeExtend::uniqidNumber(20, 'G');
        }
        if ($this->request->isGet()) {
            $data['cateids'] = str2arr($data['cateids'] ?? '');
            $this->cates = ShopGoodsCate::treeTable(true);
        }
        if ($this->request->isPost()) {
            if (empty($data['cover'])) $this->error('商品图片不能为空！');
            if (empty($data['slider'])) $this->error('轮播图片不能为空！');
            if (empty($data['goods_name'])) $this->error('商品名称不能为空！');
            if (empty($data['goods_price'])) $this->error('商品价格不能为空！');
            $cate_id = explode(',',$data['cateids']);
            $data['one_cate_id'] = $cate_id[1]??0;//一级分类id
            $data['two_cate_id'] = $cate_id[2]??0;//一级分类id
        }
    }

    /**
     * 商品上下架
     * @auth true
     */
    public function state()
    {
        GoodsModel::mSave($this->_vali([
            'status.in:0,1'  => '状态值范围异常！',
            'status.require' => '状态值不能为空！',
        ]), 'code');
    }

    /**
     * 删除商品数据
     * @auth true
     */
    public function remove()
    {
        GoodsModel::mSave($this->_vali([
            'deleted.in:0,1'  => '状态值范围异常！',
            'deleted.require' => '状态值不能为空！',
        ]), 'code');
    }

    /**
     * 编辑商品数据
     * @auth true
     */
    public function edit()
    {
        $this->mode = 'edit';
        $this->title = '编辑商品数据';
        GoodsModel::mForm('form', 'code');
    }
//
//    /**
//     * 复制编辑商品
//     * @auth true
//     */
//    public function copy()
//    {
//        $this->mode = 'copy';
//        $this->title = '复制编辑商品';
//        GoodsModel::mForm('form', 'code');
//    }

}